<?php
namespace App\Services\Adv\Adv;

use App\Models\Adv\Adv;
use App\Services\Log\Log\LogLogic;
use App\Services\Manage\ManageIndex;
/**
 * @Author: 蒋哲
 * @Date:   2017-05-27 16:25:06
 * @Last Modified by:   ws
 * @Last Modified time: 2018-06-12
 */
class AdvLogic
{
    // 添加新的广告位
    public function addAdv($data)
    {
        $data['status'] = 1;
        $data['created_at'] = date('Y-m-d H:i:s', time());
        $data['updated_at'] = date('Y-m-d H:i:s', time());

        $rst = Adv::insertGetId($data);
        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 1;
            $logData['record'] = "广告";
            (new LogLogic())->addLog($logData);
        }

        return auto_return(['id' => $rst], '广告位添加成功！', '广告位添加失败！');
    }

    // 获取某个广告位详情
    public function advInfo($data): array
    {
        $rst = Adv::where('id', $data['id'])->first();

        return auto_return($rst, '广告详情获取成功！', '广告位不存在');
    }

    // 某广告位信息编辑修改
    public function advEdit($data)
    {
        $data['updated_at'] = date('Y-m-d H:i:s', time());

        $rst = Adv::where('id', $data['id'])->update($data);

        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 2;
            $logData['record'] = "广告";
            (new LogLogic())->addLog($logData);
        }

        return auto_return($rst, '广告位信息更改成功！', '广告位信息更改失败');
    }

     // 启用某个广告位
    public function advEnable($data): array
    {
        $status['status'] = 1;
        $status['updated_at'] = date('Y-m-d H:i:s', time());
        $rst = Adv::where('id', $data['id'])->update($status);
        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 2;
            $logData['record'] = "广告";
            (new LogLogic())->addLog($logData);
        }

        return auto_return($rst, '广告位启用成功！', '广告位启用失败');
    }
    // 禁用某个广告位
    public function advDisable($data): array
    {
        $status['status'] = 0;
        $status['updated_at'] = date('Y-m-d H:i:s', time());

        $rst = Adv::where('id', $data['id'])->update($status);
        if ($rst) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 2;
            $logData['record'] = "广告";
            (new LogLogic())->addLog($logData);
        }

        return auto_return($rst, '广告位禁用成功！', '广告位禁用失败');
    }

     // 获取某个分类广告位 非删除
    public function advClassList($data): array
    {
        $_where = [
            ['adv.status', '=', 1]
        ];
        $data['c_id'] && $_where[] = ['adv.c_id', '=', $data['c_id']];
        $arr = Adv::leftJoin('adv_classify', 'adv_classify.id', '=', 'adv.c_id')
            ->select('adv.*')
            ->where($_where)
            ->orderBy('order', 'asc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();
        return auto_return_page($arr);
    }
     // 不同状态广告位列表获取
    public function advDifferenceStatus($data): array
    {
         $arr = Adv::where([['status', '=', 1],['audit_status', '=', $data['audit_status']]])
            ->orderBy('id', 'desc')
            ->paginate($data['listRows'], null, null, $data['page'])
            ->toArray();
        return auto_return_page($arr);
    }

    // 全部广告位列表获取
    public function advList()
    {
        $list = Adv::where('status', '<>', -1)->paginate(10);

        return auto_success($list);
    }
     // 规定上架时间内 正常状态 审核通过的广告位等
    public function advTimeLimit($data): array
    {

        if (!empty($data['action'])) {
            $rst  = (new AdvClassifyLogic())::where('adv_classify.action', '=', $data['action'])->first();
            if ($rst == null) {
                return auto_failure($data, '分类标识不存在！');
            }
            $data['c_id'] = $rst['id'];
        }
        $time = time();
        $arr = Adv::where([['c_id', '=', $data['c_id']],['status', '=', 1],['audit_status','=',1],['start_time','<=',$time ],['end_time','>=',$time]])
            ->limit($data['limit'])
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        return auto_return($arr);
    }
    //前台广告位列表
    public function normalAdvList()
    {
        $_where['status'] = 1;
        $arr = Adv::where($_where)
            ->orderBy('order', 'desc')
            ->get()
            ->toArray();
        return auto_return($arr);
    }

}
